GPPPARAMS = -m32 -fno-use-cxa-atexit -fleading-underscore -fno-exceptions -fno-builtin -nostdlib -fno-rtti -fno-pie
ASPRAMS = --32
LDPARAMS = -melf_i386 -no-pie
# 
objects = loader.o gdt.o port.o interruptstubs.o interrupts.o kernel.o keyboard.o mouse.o

%.o: %.cpp
	g++ ${GPPPARAMS} -o $@ -c $<

%.o :%.s
	as ${ASPRAMS} -o $@ $<

kern.bin: linker.ld ${objects}
	ld ${LDPARAMS} -T $< -o $@ ${objects}

install: kern.bin
	sudo cp $< /boot/kern.bin

kern.iso: kern.bin
	mkdir iso
	mkdir iso/boot
	mkdir iso/boot/grub
	cp $< iso/boot
	echo 'set timeout = 0' > iso/boot/grub/grub.cfg
	echo 'set default = 0' >> iso/boot/grub/grub.cfg
	echo '' >> iso/boot/grub/grub.cfg
	echo 'menuentry "my os" {' >> iso/boot/grub/grub.cfg
	echo '	multiboot /boot/kern.bin' >> iso/boot/grub/grub.cfg
	echo '	boot' >> iso/boot/grub/grub.cfg
	echo '}' >> iso/boot/grub/grub.cfg
	grub-mkrescue --output=$@ iso
	rm -rf iso

run: kern.iso
	(killall virtualboxvm && sleep 1) || true
	virtualboxvm --startvm "kern" &

.PHONY: clean
clean: 
	rm -rf ${objects} kern.iso kern.bin
